<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Controllers
 * @see 		Ajax
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Ajax extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		if (!$this->input->is_ajax_request())
		{
		   exit('No direct script access allowed');
		}
		if($this->session->userdata('logged_in') != true)
		{
			redirect();
		}
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		$this->load->model('ajax_model', 'ajax', true);
	}

	public function index()
	{
		redirect();
	}

	public function del_restore_item()
	{
		$table = $this->input->post('table', true);
		$id = $this->input->post('id', true);
		$mode = $this->input->post('mode', true);
		if (!$table || !$id || !$mode || ($table == 'users' && $id == 1))
		{
			echo 0;
			return false;
		}
		if ($mode == 'del' || $mode == 'remove')
		{
			if ($this->ajax->check_cat_child($table, $id) > 0)
			{
				echo 2;
				return false;
			}
		}
		$name = $this->input->post('name', true);
		$this->ajax->del_restore_item($table, $id, $mode, $name);
		echo 1;
		return false;
	}

	public function change_config()
	{
		$keyword = $this->input->post('keyword', true);
		$value = $this->input->post('value', true);
		if (!$keyword)
		{
			echo 0;
			return false;
		}
		$this->ajax->change_config($keyword, $value);
		echo 1;
		return false;
	}

	public function change_status()
	{
		$table = $this->input->post('table', true);
		$id = $this->input->post('id', true);
		$status = $this->input->post('status', true);
		$field = $this->input->post('field', true);
		$one_select = $this->input->post('one_select', true);
		if(!$table || !$id || !$field)
		{
			echo 0;
			return false;
		}
		$this->ajax->change_status($table, $id, $field, $status, $one_select);
	    echo 1;
	    return false;
	}

	public function delete_image()
	{
		$table = $this->input->post('table', true);
		$id = $this->input->post('id', true);
		$field = $this->input->post('field', true);
		if (!$table || !$id || !$field)
		{
			echo 0;
			return false;
		}
		$this->ajax->delete_image($table, $id, $field);
		echo 1;
		return false;
	}

	public function sort_order()
	{
		$IDs = $this->input->post('IDs', true);
		$table = $this->input->post('table', true);
		$field = $this->input->post('field', true);
		$cat = $this->input->post('cat', true);
		$orderby = $this->input->post('orderby', true);
		$ordermode = strtolower($this->input->post('ordermode', true));
		if (!$table || !$IDs)
		{
			echo 0;
			return false;
		}
		$this->ajax->sort_order($table, $IDs, $field, $cat, $orderby, $ordermode);
		echo 1;
		return false;
	}

	public function sort_nestable()
	{
	    $data = json_decode($this->input->post('dataSort', true));
	    $table = $this->input->post('tableSort', true);
	    if ($this->sort_allnestable($table, $data))
	    {
	    	echo 1;
	    }
	    else
	    {
	    	echo 0;
	    }
	    return false;
	}

	private function sort_allnestable($table, $data, $parent = 0)
	{
	    foreach ($data as $key => $val) {
			$this->ajax->sort_nestable($table, array('parent' => $parent, 'sort_order' => $key), $val->id);
			if (!empty($val->children))
			{
			    $this->sort_allnestable($table, $val->children, $val->id);
			}
	    }
	}

	public function update_price()
	{
		$id = $this->input->post('id', true);
		$table = $this->input->post('table', true);
		$field = $this->input->post('field', true);
		if ($table == '') $table = 'products';
		if ($id > 0 && $field != '')
		{
			$price = $this->input->post('price', true);
			echo $this->ajax->update_price($id, $field, $table, $price);
	    	return false;
		}
		else
		{
			echo '';
	    	return false;
		}
	}

	public function check_code()
	{
		$id = $this->input->post('id', true);
		$code = $this->input->post('code', true);
		$table = $this->input->post('table', true);
		if (!$code || !$table)
		{
			echo 1;
			return false;
		}
		echo $this->ajax->check_code($code, $table, $id);
		return false;
	}

	public function sig_logo()
	{
		$id = $this->input->post('id', true);
		if (!$id)
		{
			echo 0;
			return false;
		}
		$type_logo = $this->input->post('type_logo', true);
		$pos_logo = $this->input->post('pos_logo', true);
		$table = $this->input->post('table', true);
		$thumb_w = $this->input->post('thumb_w', true);
		$thumb_h = $this->input->post('thumb_h', true);
		$gallery_w = $this->input->post('gallery_w', true);
		$gallery_h = $this->input->post('gallery_h', true);
		$this->db->select('img');
		$this->db->where('id', $id);
		$query = $this->db->get($table);
		if ($query->num_rows() == 0)
		{
			echo 0;
			return false;
		}
		if ($table == 'products') $dir = PRO;
		else $dir = PROJ;
		$data = $query->row_array();
        $img_src = $dir.$data['img'];
        sig_logo($img_src, $img_src, $type_logo, $pos_logo);
        $thumb_name = $dir.'thumbs/'.$data['img'];
        make_thumb($img_src, $thumb_name, $thumb_w, $thumb_h);
        $thumb_name = $dir.'gallery/'.$data['img'];
        make_thumb($img_src, $thumb_name, $gallery_w, $gallery_h);
		echo 1;
		return false;
	}

	public function load_product()
	{
		$cat = $this->input->post('cat',  true);
		if (!$cat)
		{
			return false;
		}
		$id = $this->input->post('id',  true);
		$rows = $this->ajax->load_product($cat, $id);
	    if ($rows)
	    {
	    	foreach ($rows as $data)
	    	{
	    		echo '<option value="'.$data['id'].'" style="background-image: url('.PRO.'icon/'.$data['img1'].'); background-size: auto, cover; background-position: right 1px; background-repeat: no-repeat; height: 17px">'.$data['name_vn'].'</option>';
			}
		}
		return false;
	}

	public function ajax_upload_subscriber()
	{
		$output_dir = $this->input->post('dir', true);
		if (isset($_FILES['myfile']))
		{
			$error = $_FILES['myfile']['error'];
			$fileName = $_FILES['myfile']['name'];
			@move_uploaded_file($_FILES['myfile']['tmp_name'], $output_dir.$fileName);
			echo $output_dir.$fileName;
		}
	}

	public function ajax_upload_image()
	{
		$fileName = $this->input->post('fileName', true);
		$fileName = url_title(viet_decode($fileName), '-', true);
		$output_dir = $this->input->post('dir', true);
		$thumb_w = $this->input->post('thumb_w', true);
		$thumb_h = $this->input->post('thumb_h', true);
		$gallery_w = $this->input->post('gallery_w', true);
		$gallery_h = $this->input->post('gallery_h', true);
		if (isset($_FILES['myfile']))
		{
			$error = $_FILES['myfile']['error'];
			$ext = end(explode('.', $_FILES['myfile']['name']));
			$fileName = url_title(viet_decode(str_replace('.'.$ext, '', $fileName))).'-'.microtime(true).'.'.$ext;
			@move_uploaded_file($_FILES['myfile']['tmp_name'], $output_dir.$fileName);
			$full_path = $output_dir.$fileName;
			if ($thumb_w) make_thumb($full_path, $output_dir.'thumbs/'.$fileName, $thumb_w, $thumb_h);
		    if ($gallery_w) make_thumb($full_path, $output_dir.'gallery/'.$fileName, $gallery_w, $gallery_h);
			echo $output_dir.$fileName;
		}
	}

}

/* End of file ajax.php */
/* Location: ./application/controllers/ajax.php */